from flask import Flask, abort, request, jsonify
from log.Logger import Logger
import difflib
import base64
app = Flask(__name__)

log = Logger("D:/logs/webservice.log", level='debug')


@app.route('/', methods=['GET', 'POST'])
def home():
    return '<h1>hello world</h1>'


@app.route('/compare_str/', methods=['Get'])
def compare_str():
    if not request.args or 'or_str' not in request.args or 'des_str' not in request.args:
        return jsonify("error")
    or_str = request.args['or_str']
    log.logger.info("or_str" + or_str)
    des_str = request.args['des_str']
    radio = difflib.SequenceMatcher(None, or_str, des_str).quick_ratio()
    log.logger.info("or_str" + des_str)
    return str(radio)


@app.route('/compare_strs/', methods=['Post'])
def compare_strs():
    log.logger.info("compare_strs start")
    ord_str = request.form.get('ord_str')
    # ord_str = base64.b64decode(ord_str.encode('utf-8'))
    des_strs = str(request.form.get('des_strs'))
    # des_strs = str(base64.b64decode(des_strs.encode('utf-8')))
    log.logger.info("ord_str" + ord_str)
    log.logger.info("des_strs" + des_strs)

    des_array = des_strs.split('%||%')
    print(des_array)
    results = []
    for des_str in des_array:
        radio = difflib.SequenceMatcher(None, ord_str, des_str).quick_ratio()
        result = {"str": des_str, "radio": radio}
        results.append(result)

    log.logger.info("compare_strs end")
    return jsonify(results), 200


if __name__ == '__main__':
    app.run(
        host='0.0.0.0',
        port=50500,
        debug=True
    )
